<template>
  <div>
    <!-- 表格 -->
    <el-table
      :data="tableData"
      :border="border"
      :stripe="stripe"
      :size="size"
      :height="height"
      :max-height="maxHeight"
      :row-key="rowKey"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <!-- 动态列 -->
      <template v-for="column in columns" :key="column.prop">
        <el-table-column
          :prop="column.prop"
          :label="column.label"
          :width="column.width"
          :min-width="column.minWidth"
          :align="column.align || 'center'"
          :header-align="column.headerAlign || 'center'"
          :sortable="column.sortable || false"
          :fixed="column.fixed"
        >
          <!-- 自定义列模板 -->
          <template v-if="column.slot" #default="scope">
            <slot :name="column.slot" :row="scope.row" :$index="scope.$index" />
          </template>
          <template v-else #default="scope">
            <template v-if="scope.row[column.prop] || isNumber(scope.row[column.prop])">
              {{ scope.row[column.prop] }}
            </template>
            <template v-else>--</template>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-if="showPagination"
      :background="background"
      class="mt-16 flex justify-end items-center"
      :current-page="currentPage"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      :layout="paginationLayout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
// 定义列的类型
import { isNumber, isString } from "@/utils";

export interface TableColumn {
  // 字段
  prop: string;
  // 名称
  label: string;
  // 宽度
  width?: string | number;
  // 最小宽度
  minWidth?: string | number;
  // 对齐方式
  align?: "left" | "center" | "right";
  // 表头对齐方式
  headerAlign?: "left" | "center" | "right";
  // 表头排序
  sortable?: boolean;
  // 是否展示
  show?: boolean;
  // 悬浮方向
  fixed?: boolean | "left" | "right";
  // 用于自定义列内容的插槽名称
  slot?: string;
}

// 定义组件的 Props
const props = defineProps({
  tableData: {
    type: Array as PropType<any[]>,
    required: true,
  },
  columns: {
    type: Array as PropType<TableColumn[]>,
    required: true,
  },
  border: {
    type: Boolean,
    default: true,
  },
  stripe: {
    type: Boolean,
    default: false,
  },
  size: {
    type: String as PropType<"" | "default" | "small" | "large">,
    default: "default", // 默认值改为 'default'
  },
  height: {
    type: [String, Number],
    default: null,
  },
  maxHeight: {
    type: [String, Number],
    default: null,
  },
  rowKey: {
    type: String,
    default: "id",
  },
  showPagination: {
    type: Boolean,
    default: true,
  },
  background: {
    type: Boolean,
    default: true,
  },
  currentPage: {
    type: Number,
    default: 1,
  },
  pageSize: {
    type: Number,
    default: 10,
  },
  pageSizes: {
    type: Array as PropType<number[]>,
    default: () => [10, 20, 50, 100],
  },
  paginationLayout: {
    type: String,
    default: "total, sizes, prev, pager, next, jumper",
  },
  total: {
    type: Number,
    default: 0,
  },
});

// 定义事件
const emit = defineEmits(["selection-change", "row-click", "page-change", "size-change"]);

// 处理分页大小变化
const handleSizeChange = (size: number) => {
  emit("size-change", size);
};

// 处理当前页码变化
const handleCurrentChange = (page: number) => {
  emit("page-change", page);
};

// 处理表格行选中事件
const handleSelectionChange = (selection: any[]) => {
  emit("selection-change", selection);
};

// 处理表格行点击事件
const handleRowClick = (row: any, column: any, event: Event) => {
  emit("row-click", row, column, event);
};
</script>

<style scoped></style>
